#include <stdio.h>
#include "cmsis_os2.h"
#include "ohos_run.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "driver/gpio.h"


#define SYS_DELAY_TICKS 200
#define TASK_STACK_SIZE 4096
#define TASK_PRIO 25


//配置引脚
#define GPIO_RT    GPIO_NUM_27
#define GPIO_BUZ   GPIO_NUM_16

void gpio_init(void)
{ 
    //配置GPIO为输入
    gpio_config_t gpio_rt = {
        .intr_type=GPIO_INTR_DISABLE,
        .mode=GPIO_MODE_INPUT,
        .pin_bit_mask = 1ULL << GPIO_RT,
        .pull_down_en = GPIO_PULLDOWN_ENABLE,
        .pull_up_en =GPIO_PULLUP_DISABLE 
    };
    
    gpio_config(&gpio_rt);

    //配置为输出
    gpio_config_t gpio_buz = {
        .intr_type = GPIO_INTR_DISABLE, //关闭中断
        .mode = GPIO_MODE_OUTPUT,        //输出模式
        .pin_bit_mask = 1ULL << GPIO_BUZ,  //16引脚
        .pull_down_en = GPIO_PULLDOWN_DISABLE,  //关闭下拉
        .pull_up_en = GPIO_PULLUP_ENABLE        //开启上拉
    };

    gpio_config(&gpio_buz);
    printf("gpio init ok!");
}

void gpio_test(void)
{
   
    int temp;
    gpio_init();
    gpio_set_level(GPIO_BUZ,0);
    while (1){
        
        //设置输入
        temp = gpio_get_level(GPIO_RT);
        printf("jj level is %d\r\n",temp);   
        usleep(2500 * 1000);  //延时
    }
}

static void gpio_example_task(void)
{
    osThreadAttr_t attr;
    int g_taskID;

    attr.name = "adc_test";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = TASK_STACK_SIZE;
    attr.priority = TASK_PRIO;
    g_taskID = osThreadNew((osThreadFunc_t)gpio_test, NULL, &attr);
    if (g_taskID == NULL) {
        printf("Failed to create Test GPIO thread!\n");
    }
}

OHOS_APP_RUN(gpio_example_task);
